package org.wxl.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    /**
     * 从配置文件中读取上传根目录
     */
    @Value("${file.upload-root}")
    private String uploadRoot;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射物理路径到虚拟路径
        registry.addResourceHandler("/api/uploads/**")
                .addResourceLocations("file:" + uploadRoot.replace("\\", "/") + "/");
    }
}
